package com.wanpinghui.supplier.object;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.message.nim.NimManager;
import com.wanpinghui.supplier.widgets.TitlePanelLayout;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.common.dialog.LoadingDialog;
import com.wanpinghui.wphlibrary.model.base.FileUploadManager;
import com.wanpinghui.wphlibrary.model.base.bean.ImageBean;
import com.wanpinghui.wphlibrary.model.object.ObjectService;
import com.wanpinghui.wphlibrary.widgets.ThrottleButton;
import com.yunshl.yunshllibrary.permission.MPermission;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import me.nereo.multi_image_selector.PhotoPickManager;
import retrofit.callback.YRequestCallback;

/***
 * 功能描述:完成项目申报
 * 作者:qiujialiu
 * 时间:2017/12/1
 ***/
@ContentView(R.layout.layout_activity_report)
public class ReportActivity extends BaseActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
    private static final int REQUEST_SELECT_PHOTO = 1;
    @ViewInject(R.id.tpl_title)
    private TitlePanelLayout mLayoutTitle;
    @ViewInject(R.id.iv_select)
    private ImageView mImageViewSelect;
    @ViewInject(R.id.btn_submit)
    private ThrottleButton mButtonSubmit;
    @ViewInject(R.id.iv_clear)
    private ImageView mImageViewClear;

    private String photoPath;
    private long demandId;
    private boolean isUploading;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        if (getIntent() != null) {
            demandId = getIntent().getLongExtra("id", 0);
        }
    }

    @Override
    public void bindEvents() {
        mImageViewSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isUploading) {
                    PhotoPickManager.selectPic(ReportActivity.this, REQUEST_SELECT_PHOTO, 1, false, 0);
                }
            }
        });

        mImageViewClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isUploading) {
                    photoPath = null;
                    mImageViewClear.setVisibility(View.GONE);
                    mImageViewSelect.setImageResource(R.mipmap.discover_icon_adjunct);
                    mButtonSubmit.setEnabled(false);
                }
            }
        });

        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mButtonSubmit.setOnThrottleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoadingDialog.Build(ReportActivity.this).setContent("上传中").show();
                isUploading = true;
                mButtonSubmit.setEnabled(false);
                WPHSdk.getService(ObjectService.class)
                        .objectReport(demandId, FileUploadManager.bitmapToString(photoPath), new YRequestCallback<ImageBean>() {
                            @Override
                            public void onSuccess(ImageBean var1) {
                                mButtonSubmit.setEnabled(true);
                                isUploading = false;
                                LoadingDialog.dismissDialog();
                                setResult(RESULT_OK);
                                finish();
                            }

                            @Override
                            public void onFailed(int var1, String message) {
                                isUploading = false;
                                mButtonSubmit.setEnabled(true);
                                LoadingDialog.dismissDialog();
                                showWarningText(message);
                            }

                            @Override
                            public void onException(Throwable var1) {
                                isUploading = false;
                                mButtonSubmit.setEnabled(true);
                                LoadingDialog.dismissDialog();
                                var1.printStackTrace();
                            }
                        });
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<String> photoList = PhotoPickManager.getPhoto(data);
        if (photoList != null && photoList.size() > 0) {
            photoPath = NimManager.qualityCompress(photoList.get(0));
            Glide.with(ReportActivity.this).load(photoPath).asBitmap().into(mImageViewSelect);
            mButtonSubmit.setEnabled(true);
            mImageViewClear.setVisibility(View.VISIBLE);
        }
    }
}
